package com.cn.pojo;

import com.cn.mapper.TeacherMapper;
import com.cn.pojo.Student;
import com.cn.pojo.Teacher;
import com.cn.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;
import java.util.List;

public class TeacherService {

    private TeacherMapper teacherMapper;
    private SqlSession sqlSession;

    public TeacherService(SqlSession sqlSession) {
        this.sqlSession = sqlSession;
        this.teacherMapper = sqlSession.getMapper(TeacherMapper.class);
    }

    public Teacher getTeacherByIdWithStudentsAndTeachers(Integer id) {
        Teacher teacher = teacherMapper.getTeacherById(id);
        if (teacher != null) {
            for (Student student : teacher.getStudentList()) {
                List<Teacher> teacherList = teacherMapper.findTeachers(student.getsId());
                student.setTeacherList(teacherList);
            }
        }
        return teacher;
    }

    public void closeSession() {
        SqlSessionUtil.closeSqlSession(sqlSession);
    }
}